﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab_Management_System
{
    /// <summary>
    /// Represents one login session. A session begins when a user logs in and ends when the user logs out.
    /// </summary>
    internal class Session
    {
        #region Fields
        private DateTime _startTime;
        //End time is for future statistics purposes.
        private DateTime _endTime;
        private TimeSpan _sessionDuration;
        private string _username;
        private bool _isTimerActive;
        #endregion

        #region Properties
        /// <summary>
        /// Gets the time that the session started.
        /// </summary>
        public DateTime StartTime
        {
            get { return _startTime; }
        }

        /// <summary>
        /// Gets the duration of the session.
        /// </summary>
        public TimeSpan SessionDuration
        {
            get { return _sessionDuration; }
        }

        /// <summary>
        /// Gets the time that the session ended.
        /// </summary>
        public DateTime EndTime
        {
            get { return _endTime; }
        }

        /// <summary>
        /// Gets the username associated with the current session.
        /// </summary>
        public string Username
        {
            get { return _username; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the session's duration is being incremented by the global timer.
        /// </summary>
        public bool IsTimerActive
        {
            get { return _isTimerActive; }
            set { _isTimerActive = value; }
        }
        #endregion

        #region Constructor
        /// <summary>
        /// Creates a new session.
        /// </summary>
        /// <param name="username">Ther username of the person starting the session.</param>
        public Session(string username)
        {
            _username = username;

            _startTime = DateTime.Now;
            _sessionDuration = new TimeSpan(0);
            
            //Create an event handler that will increment the session duration every second.
            //This event handler is attached to a global-level timer.
            Global.programClock.Tick += programClock_Tick;            
        }
        #endregion

        #region Methods
        private void programClock_Tick(object sender, EventArgs e)
        {
            if (_isTimerActive)
            {
                //Add one second to the session's duration.
                _sessionDuration = _sessionDuration.Add(TimeSpan.FromSeconds(1));
            }
        }

        /// <summary>
        /// Ends the session.
        /// </summary>
        public void StopSession()
        {
            //Log the time the session ended (may be used later for statistical purposes).
            _endTime = DateTime.Now;

            //Stop the timer and unhook the event handler.
            _isTimerActive = false;
            Global.programClock.Tick -= this.programClock_Tick;
        }
        #endregion
    }
}
